﻿--[[
    @brief    登录界面导航tab
    @author    hqzhneg
    @date    2018-06-04
--]]

ENavTabView = 
{
    sNavPnl  = "pnlNavigationTab";
    sTabList = "NavigationTabList";
    sNextBtn = "BtnNext";
    sPreBtn = "BtnPre";

    TABSTATE = {
       STATE_NOR = 0,
       STATE_SLT = 1,
    };
};

TNavTabView =
{
    InitView = function(nRoot)
        TNavTabView.dwRootID = nRoot;
        TNavTabView.InitTabList();
    end;

    UnInitView = function()
        TNavTabView.dwRootID  = 0;
        TNavTabView.pPnlView  = nil;
    end;

    InitTabList = function()
        local is_develop_ver = false;
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= game_lib then
            is_develop_ver = game_lib:Is_Develop_ver(); 
        end
        
        if base_utility.CLIENT_TYPE.CHINA_VER ==  base_utility.get_client_version_type() then
            if is_develop_ver == true then
                TNavTabView.InitTabNode("wegame_login\\UIConfig\\navigation_tab_config_dev.xml");
            else
                TNavTabView.InitTabNode("wegame_login\\UIConfig\\navigation_tab_config_cn.xml");  
            end;    
        else
            if is_develop_ver == true then
                TNavTabView.InitTabNode("wegame_login\\UIConfig\\navigation_tab_config_dev_oversea.xml");
            else
                TNavTabView.InitTabNode("wegame_login\\UIConfig\\navigation_tab_config_oversea.xml"); 
            end;            
        end
    end;

    CheckUI = function()
        if nil ~= TNavTabView.pPnlView then
            return true;
        end;

        local     nRoot = ui_utility.get_ctrl_id_by_path(
                            TNavTabView.dwRootID, 
                            ENavTabView.sNavPnl);

        if nil == nRoot or 0 == nRoot then
            TGPLog.LogE("TNavTabView", "Get pannel fail.");
            return false;
        end;

        TNavTabView.pPnlView = ui_utility.get_ctrl_ptr_by_id(nRoot);

        if nil == TNavTabView.pPnlView then
            TGPLog.LogE("TNavTabView", "Get page fail.");
            return false;
        end;

        TNavTabView.dwNextBtnID = ui_utility.get_ctrl_id_by_path(nRoot, ENavTabView.sNextBtn);
        TNavTabView.dwPreBtnID = ui_utility.get_ctrl_id_by_path(nRoot, ENavTabView.sPreBtn);

        local dwNavTabList = ui_utility.get_ctrl_id_by_path(nRoot, ENavTabView.sTabList);
        TNavTabView.pNavList = ui_utility.get_ctrl_ptr_by_path(nRoot, ENavTabView.sTabList);

        if (nil ~= TNavTabView.pNavList) then
            -- 监听事件
            ui_utility.setproperty_by_id(dwNavTabList, "OnClickCell", 
                                      "TNavTabView.OnClkNavTabCell");
            ui_utility.setproperty_by_id(dwNavTabList, "OnUpdateCell",
                                      "TNavTabView.OnUpdateCell");

            TNavTabView.UpdateNavTabList();
        end;

        return true;
    end;

    ShowView = function(bShow)
        if not TNavTabView.CheckUI() then
            TGPLog.LogE("TNavTabView", "Check UI fail.");
            return;
        end;

        if nil == TNavTabView.pPnlView then
            TGPLog.LogE("TNavTabView", "ShowView view, loading pannel is nil.");
            return;
        end

        if bShow then
            TNavTabView.pPnlView:Show(TRUE);
        else
            TNavTabView.pPnlView:Show(FALSE);
        end;
    end;

    FocusLoginTab = function (loginType)
        for k, v in pairs(TNavTabView.zTabList.list) do
            if v.login_type == loginType then
                v.state = ENavTabView.TABSTATE.STATE_SLT;
                ui_utility.setproperty_by_id(v.ctrl_id, "Background", v.bkg_on);
            else
                v.state = ENavTabView.TABSTATE.STATE_NOR;
                ui_utility.setproperty_by_id(v.ctrl_id, "Background", v.bkg_off);
            end
        end
    end;

    OnClkNavTabCell = function (pUILib, dwCtrlID, nIndex)
        local nType = TNavTabView.zItemMap[dwCtrlID];
        TNavTabView.FocusLoginTab(nType);

        local     zParam = {};
        zParam["nPageIdx"] = TNavTabView.GetLoginPageIndexByType(nType);
        comm_center.call_service("Svr_SwitchLoginPage", zParam);
    end;

    OnUpdateCell = function (pUILib, dwCtrlID, nIndex)
        if 0 ~= dwCtrlID then
            local dwInerBtn = ui_utility.get_ctrl_id_by_path(dwCtrlID, "btnInner");
            nIndex = nIndex + 1;
            local curCellData = TNavTabView.zTabList.list[nIndex];
            if nil == curCellData then
                return;
            end

            -- 建立映射
            TNavTabView.zItemMap[dwCtrlID] = curCellData.login_type;
            curCellData["ctrl_id"] = dwInerBtn;

            local state = curCellData.state;
            local bkg = curCellData.bkg_off;
            if ENavTabView.TABSTATE.STATE_SLT == state then
                bkg = curCellData.bkg_on; 
            end
            ui_utility.setproperty_by_id(dwInerBtn, "Background", bkg);
            ui_utility.setproperty_by_id(dwCtrlID, "TextTip", curCellData.tool_tip);
        else
            TGPLog.LogI("TQuickLogin", "No need to update cell:" .. tostring(nIndex));
        end;
    end;

    UpdateNavTabList = function ()
        if nil == TNavTabView.pNavList then
            TGPLog.LogI("QKLogin", "Acc list not created.");
            return;
        end;

        if nil == TNavTabView.zTabList then
            TGPLog.LogI("QKLogin", "Account list is null.");
            return;
        end;

        -- 切换重置当前选择
        TNavTabView.curSelect = 0;

        local nCount = TNavTabView.zTabList.count;

        -- 标记开始更新数据
        TNavTabView.pNavList:BeginUpdate();

        -- 只显示一行
        TNavTabView.pNavList:SetRowCount(1);
        -- 已经加载成功本地列表
        TNavTabView.pNavList:SetItemCount(0);   -- 先清空

        TNavTabView.nHovItem = 0;
        TNavTabView.pNavList:SetItemCount(nCount);

        if (nCount <= 0) then
            TGPLog.LogI("QKLogin", "Account list is empty.");
            return;
        end;

        -- 默认选中第一个
        TNavTabView.pNavList:SetSelect(0);

        -- OK，刷新完成，更新布局
        TNavTabView.pNavList:EndUpdate();

        -- 焦点切换到账号列表上
        TNavTabView.pNavList:SetFocus();

        if TNavTabView.curSelect + TNavTabView.MAXNODE >= TNavTabView.zTabList.count then
            -- 右边没有节点了
            TNavTabView.SetNextBtnEnable(false);
        end
        if 0 == TNavTabView.curSelect then
            -- 左边没有节点了
            TNavTabView.SetPreBtnEnable(false);
        end
    end;

    OnClickNextBtn = function (pUILib, nCtrlID)
        if nil == TNavTabView.pNavList then
            return;
        end
        TNavTabView.pNavList:SetSelect(TNavTabView.curSelect + TNavTabView.MAXNODE);
        TNavTabView.curSelect = TNavTabView.curSelect + TNavTabView.MAXNODE - 1;
        if TNavTabView.curSelect + TNavTabView.MAXNODE >= TNavTabView.zTabList.count then
            -- 右边没有节点了
            TNavTabView.SetNextBtnEnable(false);
        end
        -- 设置pre按钮可以点击
        TNavTabView.SetPreBtnEnable(true);
    end;

    OnClickPreBtn = function (pUILib, nCtrlID)
        if nil == TNavTabView.pNavList then
            return;
        end
        TNavTabView.pNavList:SetSelect(TNavTabView.curSelect - (TNavTabView.MAXNODE - 1));
        TNavTabView.curSelect = TNavTabView.curSelect - (TNavTabView.MAXNODE - 1);
        if 0 == TNavTabView.curSelect then
            -- 左边没有节点了
            TNavTabView.SetPreBtnEnable(false);
        end
        -- 设置next按钮可以点击
        TNavTabView.SetNextBtnEnable(true);
    end;

    SetPreBtnEnable = function (bEnable)
        if bEnable then
            ui_utility.setproperty_by_id(TNavTabView.dwPreBtnID, "Disable", "false");
            ui_utility.setproperty_by_id(TNavTabView.dwPreBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?455,490,462,501;Center;0;");
        else
            ui_utility.setproperty_by_id(TNavTabView.dwPreBtnID, "Disable", "true");
            ui_utility.setproperty_by_id(TNavTabView.dwPreBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?525,490,532,501;Center;0;");
        end
    end;

    SetNextBtnEnable = function (bEnable)
        if bEnable then
            ui_utility.setproperty_by_id(TNavTabView.dwNextBtnID, "Disable", "false");
            ui_utility.setproperty_by_id(TNavTabView.dwNextBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?456,459,463,470;Center;0;");
        else
            ui_utility.setproperty_by_id(TNavTabView.dwNextBtnID, "Disable", "true");
            ui_utility.setproperty_by_id(TNavTabView.dwNextBtnID, "Background",
                "ONEIMAGE:client_login\\login_input.png?526,459,533,470;Center;0;");
        end
    end;

    GetLoginPageIndexByType = function (loginType)
        local pageIdx = ELoginPage.lpAccLogin;
        if TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin == loginType then
            -- 获取快速登录列表
            local zList = TLoginModule.GetQuickAccList();
            if nil ~= zList and zList.count > 0 then
                pageIdx = ELoginPage.lpQKLogin; 
            else     -- 显示帐号登录
                pageIdx = ELoginPage.lpAccLogin;
            end;
        elseif TLoginModule.LOGIN_ACCOUNT_TYPE.KWeChatLogin == loginType then
            pageIdx = ELoginPage.lpWeChatQRLogin;
        elseif TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin == loginType then
            pageIdx = ELoginPage.lpMailAccLogin;
        elseif TLoginModule.LOGIN_ACCOUNT_TYPE.KFacebookLogin == loginType then
            pageIdx = ELoginPage.lpOverseasLogin;
        end
        return pageIdx;
    end;

    InitTabNode = function (file)
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(file);
        if nil == xmlparser or nil == xmlparser.tab_info then
            return false;
        end
        TNavTabView.zTabList["list"] = {};
        local node_size = #xmlparser.tab_info.tab;
        TNavTabView.zTabList["count"] = node_size;
        if 0 == TNavTabView.zTabList["count"] then
            local tab_info = {};
            TNavTabView.GenerateTabInfo(xmlparser.tab_info.tab, tab_info);
            table.insert(TNavTabView.zTabList["list"], tab_info);
            TNavTabView.zTabList["count"] = 1;
        else
            for i = 1, node_size do
                local tab_info = {};
                local tab = xmlparser.tab_info.tab[i];
                TNavTabView.GenerateTabInfo(tab, tab_info);
                table.insert(TNavTabView.zTabList["list"], tab_info);
            end;
        end
    end;

    GenerateTabInfo = function (tab, tab_info)
        if nil == tab or nil == tab_info then
            return;
        end
        if tab ~= nil then
            tab_info["name"] = tab["@name"];
            if nil ~= tab.login_type then
                tab_info["login_type"] = tonumber(tab.login_type["@value"]);
            end
            if nil ~= tab.state then
                tab_info["state"] = tonumber(tab.state["@value"]);
            end
            if nil ~= tab.bkg_on then
                tab_info["bkg_on"] = tab.bkg_on["@value"];
            end
            if nil ~= tab.bkg_off then
                tab_info["bkg_off"] = tab.bkg_off["@value"];
            end
            if nil ~= tab.tool_tip then
                tab_info["tool_tip"] = LoadStr(tab.tool_tip["@value"]);            
            end
        end
    end;

    -- 加载界面
    dwRootID  = 0;
    pPnlView  = nil;
    pNavList  = nil;
    dwPreBtnID = 0;
    dwNextBtnID = 0;

    curSelect = 0;
    zTabList = {};

    MAXNODE = 4;

    -- CtrlID <==> type
    zItemMap = {};
};